/*
 * @Author: LSC
 * @Date: 2021-04-22 12:09:47
 * @LastEditTime: 2021-05-08 16:51:47
 */

const Mock = require('mockjs')
var dayjs = require('dayjs')
const orderList = []
const count = 3
let initList = [
  {
    productName: 'DIY Card',
    orderStatus: { value: '1', label: 'Unpay' }
  },
  {
    productName: 'DIY Card',
    orderStatus: { value: '2', label: 'Reviewing' }
  },
  {
    productName: 'key fobs',
    orderStatus: { value: '4', label: 'Shipped' }
  },
]
for (let i = 0; i < count; i++) {
  orderList.push(Mock.mock({
    id: i,
    orderTime: new Date(),
    orderNumber: 237827492001 + i,
    productName: initList[i].productName,
    quantity: '$14.00  ×1',
    grandTotal: '$14.00',
    orderStatus: initList[i].orderStatus,
  }))
}
// { value: '2', label: 'Reviewing' },
// { value: '3', label: 'Waiting delivery' },
// { value: '4', label: 'Shipped' },
// { value: '5', label: 'Order cancelled' }
module.exports = [
  // username search
  {
    url: '/vue-element-admin/get/orderList',
    type: 'get',
    response: config => {

      let { page = 1, limit = 10, orderTime0, orderTime1, orderNumber, productName, orderStatus, orderType = 'type0' } = config.query
      let searchArr = JSON.parse(JSON.stringify(orderList))
      //条件搜索
      if (orderType == 'type0') {
        searchArr = searchArr.filter(item => item.orderStatus.label != 'Shipped')
      } else {
        searchArr = searchArr.filter(item => item.orderStatus.label == 'Shipped')

      }
      if (orderTime0) {
        let startTime, endTime
        startTime = dayjs(orderTime0).valueOf()
        endTime = dayjs(orderTime1).add(1, 'day').valueOf()

        searchArr = searchArr.filter(item => {
          let obj = JSON.parse(JSON.stringify(item))
          let time = dayjs(obj.orderTime).valueOf()
          if (time > startTime && time < endTime) {
            return item
          }
        })
      }
      if (orderNumber) {
        searchArr = searchArr.filter(item => item.orderNumber == orderNumber)
      }
      if (productName) {
        productName = productName.replace(/\+/g, " ");
        searchArr = searchArr.filter(item => item.productName === productName)
      }
      if (orderStatus) {
        searchArr = searchArr.filter(item => item.orderStatus.value == orderStatus)
      }
      const pageList = searchArr.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        count: searchArr.length,
        data: pageList
      }
    }
  }, {
    url: '/vue-element-admin/getOrderById',
    type: 'get',
    response: config => {
      const { id } = config.query
      let searchArr = JSON.parse(JSON.stringify(orderList))
      searchArr = searchArr.filter(item => item.id == id)[0]
      return {
        code: 20000,
        data: searchArr
      }
    }
  },

]
